package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDAO;
    @Override
    public void importExcel(List<OrderSetting> orderSettingList) {
        if (orderSettingList!=null&&orderSettingList.size()>0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Long orderNum = orderSettingDAO.findOrderSettingByDate(orderSetting.getOrderDate());
                if (orderNum > 0) {
//                如果已经有预约的数据  那么就进行更新而不是添加
                    orderSettingDAO.updateOrderSetting(orderSetting);
                } else {
//                如果还没有对应日期的数据，那么就进行添加
                    orderSettingDAO.addOrderSetting(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderYearAndMonth(String date) {
//        获取对应的月数的最后一天 是几号
        String[] dateArr = date.split("-");
        String yearS = dateArr[0];
        String monthS = dateArr[1];
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(calendar.YEAR, Integer.parseInt(yearS));
        calendar.set(calendar.MONTH, Integer.parseInt(monthS));
        calendar.set(calendar.DATE,1);
        calendar.add( calendar.DATE, -1);
        int i = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println("本月的最后一天是"+i);

//        通过DAO 获取到本月预约的数据
        Map<String, String> condition = new HashMap<>();
        condition.put("begin", yearS+"-"+monthS+"-1");
        condition.put("end", yearS+"-"+monthS+"-"+i);
        List<OrderSetting> listOfOrderSetting = orderSettingDAO.getOrderYearAndMonth(condition);
        List<Map> resultMap = new ArrayList<>();
        if (listOfOrderSetting!=null&&listOfOrderSetting.size()>0){
            for (OrderSetting orderSetting : listOfOrderSetting) {
                Map map = new HashMap<>();
                map.put("date", orderSetting.getOrderDate().getDate());
                map.put("number", orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                resultMap.add(map);
            }
        }

        return resultMap;
        // this.leftobj = [
        //     { date: 1, number: 120, reservations: 1 },
        //     { date: 3, number: 120, reservations: 1 },
        //     { date: 4, number: 120, reservations: 120 },
        //     { date: 6, number: 120, reservations: 1 },
        //     { date: 8, number: 120, reservations: 1 }
        // ];
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Long count = orderSettingDAO.findOrderSettingByDate(orderSetting.getOrderDate());
        if (count>0){
            orderSettingDAO.updateOrderSetting(orderSetting);
        }else {
            orderSettingDAO.addOrderSetting(orderSetting);
        }
    }
}
